﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HexagonWeb.DAL;
using HexagonWeb.IBIL;
using HexagonWeb.Models;

namespace HexagonWeb.BIL
{
    public class AirlineService : BaseService<Airline>, InterfaceAirlineService
    {
         public AirlineService() : base(RepositoryFactory.AirlineRepository) { }

        public bool Exist(string num)
        {
            return CurrentRepository.Exist(u => u.Num == num);
        }

        public Airline Find(int id)
        {
            return CurrentRepository.Find(u => u.ID == id);
        }

        public IQueryable<Airline> FindPageList(int pageIndex, int pageSize, out int totalRecord, int order)
        {
            bool _isAsc = true;
            string _orderName = string.Empty;
            switch (order)
            {
                default:
                    _isAsc = true;
                    _orderName = "ID";
                    break;
            }
            return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, _orderName, _isAsc);
        }


        public IQueryable<Airline> FindPageList(int pageIndex, int pageSize, out int totalRecord,DateTime Today,  int order)
        {
            bool _isAsc = true;
            string _orderName = string.Empty;
            switch (order)
            {
                default:
                    _isAsc = true;
                    _orderName = "ID";
                    break;
            }
            return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => u.WeekDay == Today, _orderName, _isAsc);
        }

        public IQueryable<Airline> Order(IQueryable<Airline> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.ArriveTime);
                    break;
            }
            return entitys;
        }


        public IQueryable<Airline> FindPageList(out int totalRecord, int pageIndex, int pageSize,
            string EscapeDistrict, string ArriveDistrict, Nullable<DateTime> fromDate, Nullable<DateTime> toDate, int orderCode)
        {
            IQueryable<Airline> _airlineModels = CurrentRepository.Entities;
            if (!string.IsNullOrEmpty(EscapeDistrict))
                _airlineModels = _airlineModels.Where(cm => cm.EscapeDistrict.Contains(EscapeDistrict));
            if (!string.IsNullOrEmpty(ArriveDistrict))
                _airlineModels = _airlineModels.Where(cm => cm.ArriveDistrict.Contains(ArriveDistrict));
            if (fromDate != null)
                _airlineModels = _airlineModels.Where(cm => cm.WeekDay >= fromDate);
            if (toDate != null)
                _airlineModels = _airlineModels.Where(cm => cm.WeekDay <= toDate);
            _airlineModels = Order(_airlineModels, orderCode);
            totalRecord = _airlineModels.Count();
            return PageList(_airlineModels, pageIndex, pageSize).AsQueryable();
        }
    }
}